package kt.com.bilin.coin.ui.activity

import android.media.MediaPlayer
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemBsAudioBinding
import com.bilin.coin.databinding.ItemBsVideoBinding
import kotlinx.android.synthetic.main.activity_base_refresh.*
import kt.com.bilin.coin.bean.BSVideoBean
import kt.com.bilin.coin.common.*
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder


@Route(path = PATH_BS_VIDEO)
class BSVideoActivity : BaseRefreshActivity<BSVideoBean>() {
    private var mMediaPlayer: MediaPlayer? = null
    override fun initView() {
        mBaseRecyclerAdapter = BSVideoAdapter()
        super.initView()
        titleView.titleStr = R.string.text_business_school_video.getString()
        titleView.isShowLine = true
        refreshLayout.setEnableRefresh(false)
        refreshLayout.setEnableLoadMore(false)
        rootView.setBackgroundColor(R.color.white.toColor())
    }

    override fun onReload() {
        launchUI({
            mutableListOf<BSVideoBean>().apply {
                add(BSVideoBean("群内活跃", MP4_1, R.mipmap.cover_test1, "2020-10-05 14:23:14"))
            }
        }, mRefreshHelper.handApiObserver())
    }

    override fun onDestroy() {
        super.onDestroy()
        mMediaPlayer?.apply {
            stop()
            release()
            mMediaPlayer = null
        }
    }

    inner class BSVideoAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemBsVideoBinding>, BSVideoBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemBsVideoBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemBsVideoBinding>, bean: BSVideoBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    tvTitle.text = title
                    tvTime.text = time
                    ivCover.setImageResource(cover)
                    setViewClick(position, root)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            ARouterNavigation(PATH_SIMPLE_VIDEO) {
                withString(SimpleVideoViewPlayerActivity.VIDEO_URL, getItem(position).link)
            }
        }
    }
}